package com.lck.tutor.controller;

import com.lck.tutor.common.BaseResponse;
import com.lck.tutor.common.ErrorCode;
import com.lck.tutor.common.Result;
import com.lck.tutor.model.domain.Blog;
import com.lck.tutor.model.domain.User;
import com.lck.tutor.service.BlogService;
import com.lck.tutor.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author:鹿存岿
 * @date 2022/11/07 19:45
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<?> saveBlog(@RequestBody Blog blog, HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        return blogService.saveBlog(blog,userId);
    }

    @GetMapping("/getBlogsByUserId/{id}")
    public BaseResponse<List<Blog>> getBlogsByUserId(@PathVariable("id") Long id){
        List<Blog> blogs = blogService.query().eq("userId", id).list();
        if (blogs == null){
            return Result.fail(ErrorCode.NULL_ERROR);
        }
        return Result.success(blogs);
    }
}
